import os
import os.path
import xml.dom.minidom
import xml.etree.cElementTree as et
import matplotlib.pyplot as plt

 
 
path="all_xml_2nd_7998_20220729"
files=os.listdir(path)
s=[]

square_list = [str('>16*16'), str('16*16 - 32*32'), str('32*32 - 96*96'), str('96*96<')]
square_list_16 = []
square_list_32 = []
square_list_96 = []
square_list_96_ = []
object_list = []
side_list = []
# =============================================================================
# extensional filename
# =============================================================================
def file_extension(path): 
    return os.path.splitext(path)[1] 
 
for xmlFile in files: 
    if not os.path.isdir(xmlFile): 
        if file_extension(xmlFile) == '.xml':
            # print(os.path.join(path,xmlFile))
            tree=et.parse(os.path.join(path,xmlFile))
            root=tree.getroot()
            filename=root.find('filename').text
#            print("filename is", path + '/' + xmlFile)
            for Object in root.findall('object'):
#                name=Object.find('name').text
#                print("Object name is ", name)
                object_list.append(Object)
                bndbox=Object.find('bndbox')
                xmin=bndbox.find('xmin').text
                ymin=bndbox.find('ymin').text
                xmax=bndbox.find('xmax').text
                ymax=bndbox.find('ymax').text
                square = (int(ymax)-int(ymin)) * (int(xmax)-int(xmin))
                if square <= 16*16:
                    square_list_16.append(square)
                if 16*16 < square <= 32*32:
                    square_list_32.append(square)
                if 32*32 < square <= 96*96:
                    square_list_96.append(square)
                if square > 96*96:
                    square_list_96_.append(square)

                
            for Object in root.findall('size'):
                side1=Object.find('width').text
                side2=Object.find('height').text
                side_list.append(int(side1))
                side_list.append(int(side2))
#                print(xmin,ymin,xmax,ymax)
                # print(square)

square_list_num = [len(square_list_16), len(square_list_32), len(square_list_96), len(square_list_96_)]
percent_list = [float(round(len(square_list_16)/len(object_list)*100, 2)), float(round(len(square_list_32)/len(object_list)*100, 2)), 
                float(round(len(square_list_96)/len(object_list)*100, 2)), float(round(len(square_list_96_)/len(object_list)*100, 2))]

#刻度距离坐标轴的距离调整
plt.tick_params(pad = 0.03)  #通过pad参数调整距离

#data_collection_location
plt.bar(square_list, square_list_num, color = 'g', align =  'center')
plt.title('Bosch 2D Detection')
plt.text(len(square_list)/2-0.5, max(square_list_num)*1.15, list(square_list_num), ha='center', fontsize=12)
plt.text(len(square_list)/2-0.5, max(square_list_num)*1.25, "percent is %s"% list(percent_list), ha='center', fontsize=12)
plt.text(len(square_list)/2-0.5, max(square_list_num)*1.35, "bbox of dataset is %s"% len(object_list), ha='center', fontsize=12)
plt.text(len(square_list)/2-0.5, max(square_list_num)*1.45, "imgs of dataset is %s"% len(files), ha='center', fontsize=12)
plt.ylabel('Location axis',fontsize=11) 
plt.xlabel('Num axis',fontsize=11,linespacing = 0.3, labelpad = 0.3) 
plt.savefig('square_list_num.png', bbox_inches='tight')
plt.close()